﻿using AppSocketModel;
using AppSocketService.Device;
using AppSocketService.Fence;
using AppSocketService.Location;
using BCGPSCommon;
using BCGPSMongoData;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace AppSocketControl.GpsData.DataDispatch
{
    public class DispatchLocation
    {

        private readonly IRedisService redisService;
        private readonly ILocationService locationService;
        private readonly IFenceService fenceService;

        private readonly MongoDBCommand<GpsInfoDto> MongoDBCommandHistoryData = new MongoDBCommand<GpsInfoDto>("HistoryGpsData");
        private readonly MongoDBCommand<AlarmNotifyHistoryDto> MongoDBCommandAlarmHistory = new MongoDBCommand<AlarmNotifyHistoryDto>("AlarmNotifyHistory");

        private bool IsAlarm;
        private LocationDto RedisOldLocation;
        private List<PreviousAlarmDto> PreAlarmTimeList { set; get; }
        private IDeviceService deviceService = new DeviceService(new RedisService());

        public DispatchLocation()
        {
            redisService = new RedisService();
            locationService = new LocationService(redisService);
            fenceService = new FenceService();
        }

        public void Run(string[] dataArr)
        {
            GpsInfoDto gpsInfoDto = Split(dataArr);
            if (gpsInfoDto != null)
            {
                IsAlarm = false;
                Save(gpsInfoDto);
                AlarmNotify(gpsInfoDto);
                SaveRedis(gpsInfoDto);
            }
        }

        #region 分解数据
        private GpsInfoDto Split(string[] dataArr)
        {
            if (dataArr[7].Contains("GNRMC"))
                return SplitGps(dataArr);
            if (dataArr[7].Contains("+CLBS:"))
                return SplitLbs(dataArr);
            if (dataArr[7].Contains("+LBS:"))
                return SplitLbs_WH_GM5(dataArr);
            return null;
        }


        private GpsInfoDto SplitGps(string[] dataArr)
        {
            GpsInfoDto gpsInfoDto = new GpsInfoDto();
            gpsInfoDto.Imei = dataArr[5];
            gpsInfoDto.HardwareCode = dataArr[2];
            gpsInfoDto.SysTime = DateTimeTransfrom.GetDateTime(dataArr[11]);
            gpsInfoDto.ServerTime = DateTime.Now;
            deviceService.UpdateLastDataTime(gpsInfoDto.Imei, gpsInfoDto.ServerTime.Value);

            string[] arrGps = dataArr[7].Split(',');

            if (arrGps.Length >= 13)
            {
                //有效数据
                if (arrGps[2] == "A")
                {
                    gpsInfoDto.GpsTime = new DateTime(
                        int.Parse(DateTime.Now.Year.ToString().Substring(0, 2) + arrGps[9].Substring(4, 2)),
                        int.Parse(arrGps[9].Substring(2, 2)),
                        int.Parse(arrGps[9].Substring(0, 2)),
                        int.Parse(arrGps[1].Substring(0, 2)),
                        int.Parse(arrGps[1].Substring(2, 2)),
                        int.Parse(arrGps[1].Substring(4, 2))).AddHours(8);

                    var latArr = arrGps[3].Split('.');
                    if (latArr.Length == 2)
                    {
                        gpsInfoDto.Lat = int.Parse(latArr[0].Substring(0, 2)) + double.Parse(latArr[0].Substring(2, 2) + "." + latArr[1]) / 60.00;
                    }
                    else
                        return null;
                    gpsInfoDto.LatHemi = arrGps[4];

                    var lngArr = arrGps[5].Split('.');
                    if (lngArr.Length == 2)
                    {
                        gpsInfoDto.Lng = int.Parse(lngArr[0].Substring(0, 3)) + double.Parse(lngArr[0].Substring(3, 2) + "." + lngArr[1]) / 60.00;
                    }
                    else
                        return null;
                    gpsInfoDto.LngHemi = arrGps[6];

                    gpsInfoDto.Course = double.Parse(arrGps[8]);
                    gpsInfoDto.Speed = double.Parse(arrGps[7]);


                    if (dataArr[8].Contains("ADDINFO"))
                    {
                        GetAddInfo(dataArr[8].Split(','),ref   gpsInfoDto);

                        if (gpsInfoDto == null)
                            return null;
                    }

                    if (GeneralContainer.IsUseGpsDataValidCount)
                        deviceService.UpdateGpsDataValidCount(gpsInfoDto.Imei, true,gpsInfoDto.Location);
                    return gpsInfoDto;
                }
                else
                {
                    if (GeneralContainer.IsUseGpsDataValidCount)
                        deviceService.UpdateGpsDataValidCount(gpsInfoDto.Imei, false, gpsInfoDto.Location);
                }
            }
            return null;
        }
        private void GetAddInfo(string[] addInfoArr,ref GpsInfoDto gpsInfoDto)
        { 
            foreach (var item in addInfoArr)
            {
                string[] innerInfoArr = item.Split(':');
                if (innerInfoArr.Length == 2)
                {
                    switch (innerInfoArr[0])
                    {
                        case "1": 
                            switch (innerInfoArr[1])
                            {
                                case "GPS+BD":
                                    gpsInfoDto.Location = "GPS+北斗";
                                    break;
                                case "BD":
                                    gpsInfoDto.Location = "北斗";
                                    break;
                                case "GPS":
                                    gpsInfoDto.Location = "GPS";
                                    break;
                                case "LBS":
                                    gpsInfoDto.Location = "LBS";
                                    break;
                                default:
                                    gpsInfoDto.Location = innerInfoArr[1];
                                    break;
                            }
                            break;
                        case "2": 
                            try
                            {
                                gpsInfoDto.BatteryVoltage = float.Parse(innerInfoArr[1]);
                            }
                            catch
                            {
                                gpsInfoDto = null;
                                return;
                            }
                            break;
                        case "3":
                            gpsInfoDto.WakeUpType = innerInfoArr[1];
                            break;
                        case "4":
                            gpsInfoDto.SendNo = int.Parse(innerInfoArr[1]);
                            break;
                        case "5":
                            gpsInfoDto.Len = int.Parse(innerInfoArr[1]);
                            break;
                        case "6":
                            gpsInfoDto.SingledBm = int.Parse(innerInfoArr[1]);
                            break;

                    }
                }
            } 
        }

        private GpsInfoDto SplitLbs(string[] dataArr)
        {
            GpsInfoDto gpsInfoDto = new GpsInfoDto();
            gpsInfoDto.Imei = dataArr[5];
            gpsInfoDto.SysTime = DateTimeTransfrom.GetDateTime(dataArr[11]);
            gpsInfoDto.ServerTime = DateTime.Now;
            gpsInfoDto.GpsTime = BCGPSCommon.MongoDBDateTime.GetNowUtc();
            deviceService.UpdateLastDataTime(gpsInfoDto.Imei, gpsInfoDto.ServerTime.Value);

            string[] arrGps = dataArr[7].Split(',');
            Msg.WirteLine("arrGps.Length:=" + arrGps.Length, 5);
            foreach (var item in arrGps)
            {
                Msg.WirteLine(item,10);
            }
            if (arrGps.Length >= 4)
            {
                if (arrGps[0].Contains("+CLBS:"))
                {
                    gpsInfoDto.Lng = double.Parse(arrGps[1]);
                    gpsInfoDto.Lat = double.Parse(arrGps[2]);

                    if (dataArr[8].Contains("ADDINFO"))
                    {
                        GetAddInfo(dataArr[8].Split(','), ref gpsInfoDto);

                        if (gpsInfoDto == null)
                            return null;
                    }

                    if (GeneralContainer.IsUseGpsDataValidCount)
                        deviceService.UpdateGpsDataValidCount(gpsInfoDto.Imei, true, gpsInfoDto.Location);
                    return gpsInfoDto;
                }
            }
            if (GeneralContainer.IsUseGpsDataValidCount)
                deviceService.UpdateGpsDataValidCount(gpsInfoDto.Imei, false, gpsInfoDto.Location);
            return null;
        }



        private GpsInfoDto SplitLbs_WH_GM5(string[] dataArr)
        {
            GpsInfoDto gpsInfoDto = new GpsInfoDto();
            gpsInfoDto.Imei = dataArr[5];
            gpsInfoDto.SysTime = DateTimeTransfrom.GetDateTime(dataArr[11]);
            gpsInfoDto.ServerTime = DateTime.Now;
            gpsInfoDto.GpsTime = BCGPSCommon.MongoDBDateTime.GetNowUtc();
            deviceService.UpdateLastDataTime(gpsInfoDto.Imei, gpsInfoDto.ServerTime.Value);


            string[] arrGps = dataArr[7].Replace("+LBS:", "").Split(',');
            Msg.WirteLine("arrGps.Length:=" + arrGps.Length, 5);
            foreach (var item in arrGps)
            {
                Msg.WirteLine(item, 10);
            }
            if (arrGps.Length >= 3)
            {
                gpsInfoDto.Lng = double.Parse(arrGps[0].Replace("LNG = ", ""));
                gpsInfoDto.Lat = double.Parse(arrGps[1].Replace("LAT = ", ""));
          
                if (dataArr[8].Contains("ADDINFO"))
                {
                    GetAddInfo(dataArr[8].Split(','), ref gpsInfoDto);

                    if (gpsInfoDto == null)
                        return null;
                }

                if (GeneralContainer.IsUseGpsDataValidCount)
                    deviceService.UpdateGpsDataValidCount(gpsInfoDto.Imei, true, gpsInfoDto.Location);
                return gpsInfoDto;
            }
            if (GeneralContainer.IsUseGpsDataValidCount)
                deviceService.UpdateGpsDataValidCount(gpsInfoDto.Imei, false, gpsInfoDto.Location);
            return null;
        }
        #endregion


        #region 保存数据
        private void Save(GpsInfoDto gpsInfoDto)
        {
            SaveHistory(gpsInfoDto);
            UpdateAccountLocation(gpsInfoDto);
        }

        //保存单个gps历史记录
        private void SaveHistory(GpsInfoDto gpsInfoDto)
        {
            //Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss fff")}  ,  Lng : {gpsInfoDto.Lng}  ,  Lat : {gpsInfoDto.Lat}");
            //Console.WriteLine(JsonExtensions.ObjectToJson(gpsInfoDto));
            MongoDBCommandHistoryData.InsertDocumentAsync(gpsInfoDto.ToUtcDateTime());
        }

        //更新账户GPS位置表
        private void UpdateAccountLocation(GpsInfoDto gpsInfoDto)
        { 
            locationService.UpdateGpsInfo(gpsInfoDto.ToUtcDateTime()); 
        }
         
        //更新内存GPS位置字典
        private void SaveRedis(GpsInfoDto gpsInfoDto)
        {
            Msg.WirteLine($"  {gpsInfoDto.Imei}  --- InSaveRedis!");
            Msg.WirteLine("");

            var locationDto = gpsInfoDto.GetLocationDto();

            if (IsAlarm)
                locationDto.PreAlarmDateTime = DateTime.Now;
            else
            {
                if ((RedisOldLocation != null) && (RedisOldLocation.PreAlarmDateTime != null))
                {
                    locationDto.PreAlarmDateTime = RedisOldLocation.PreAlarmDateTime;
                }
            }
            redisService.AddAsync(GeneralContainer.RedisNameSpaceIMEILocation + gpsInfoDto.Imei, locationDto);
        }
        #endregion

        #region 报警处理
        public void AlarmNotify(GpsInfoDto gpsInfoDto)
        {
            if (redisService.Exists(GeneralContainer.RedisNameSpaceIMEIAlarm + gpsInfoDto.Imei))
            {
                RedisOldLocation = redisService.Get<LocationDto>(GeneralContainer.RedisNameSpaceIMEILocation + gpsInfoDto.Imei);
                 
                var alarmDto = redisService.Get<AlarmDto>(GeneralContainer.RedisNameSpaceIMEIAlarm + gpsInfoDto.Imei);
                if (alarmDto != null)
                {
                    if (alarmDto.IsAlarm)
                    {
                        if(RedisOldLocation == null || RedisOldLocation.PreAlarmDateTime==null)
                        { 
                            SaveAlarm(gpsInfoDto, alarmDto);
                        }
                        else
                        {
                            if ((DateTime.Now - RedisOldLocation.PreAlarmDateTime.Value).TotalSeconds > alarmDto.AlarmTimeSpan * 60)
                            {
                                SaveAlarm(gpsInfoDto, alarmDto);
                            }
                        } 
                    }
                }
            }
        }
        
        //SaveAlarm
        private void SaveAlarm(GpsInfoDto gpsInfoDto, AlarmDto alarmDto )
        {
            AlarmNotifyHistoryDto alarmNotifyHistoryDto = new AlarmNotifyHistoryDto()
            {
                CreateDateTime = BCGPSCommon.MongoDBDateTime.GetNowUtc(),
                SN = BCGPSCommon.GuidGenerate.NewGuid(),
                IMEI = gpsInfoDto.Imei,
                Lng = gpsInfoDto.Lng,
                Lat = gpsInfoDto.Lat,
                Location = gpsInfoDto.Location,
            };

            alarmNotifyHistoryDto.CompanyId = alarmDto.CompanyId;
            alarmNotifyHistoryDto.AccountId = alarmDto.AccountId; 

            if (alarmDto.OverSpeedAlarm.IsAlarm)
            {
                if (gpsInfoDto.Speed > alarmDto.OverSpeedAlarm.SpeedLimit)
                {
                    alarmNotifyHistoryDto.AlarmType = AlarmType.OverSpeed;
                    alarmNotifyHistoryDto.AlarmTypeStr = AlarmType.OverSpeed.GetDescriptionString();
                    if (alarmDto.OverSpeedAlarm.AlarmNotify.AppNotify.IsAlarm)
                    {
                        alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.APP;
                        alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.APP.GetDescriptionString();
                        alarmNotifyHistoryDto.NotifyMessage = alarmDto.OverSpeedAlarm.AlarmMsgPre + gpsInfoDto.Speed.Value.ToString();
                        SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                    }
                    if (alarmDto.OverSpeedAlarm.AlarmNotify.PhoneNotify.IsAlarm)
                    {
                        alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.PHONE;
                        alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.PHONE.GetDescriptionString();
                        alarmNotifyHistoryDto.NotifyMessage = alarmDto.OverSpeedAlarm.AlarmMsgPre + gpsInfoDto.Speed.Value.ToString();
                        SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                    }
                    if (alarmDto.OverSpeedAlarm.AlarmNotify.SMSNotify.IsAlarm)
                    {
                        alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.SMS;
                        alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.SMS.GetDescriptionString();
                        alarmNotifyHistoryDto.NotifyMessage = alarmDto.OverSpeedAlarm.AlarmMsgPre + gpsInfoDto.Speed.Value.ToString();
                        SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                    }
                }
            }

            if (alarmDto.MoveAlarm.IsAlarm)
            {
                if (alarmDto.MoveAlarm.MoveDistanceLimit > 0)
                {
                    double len = GpsMath.GetDistance(RedisOldLocation.Lat, RedisOldLocation.Lng, gpsInfoDto.Lat, gpsInfoDto.Lng) * 1000;
                    if (len > alarmDto.MoveAlarm.MoveDistanceLimit)
                    {

                        alarmNotifyHistoryDto.AlarmType = AlarmType.Move;
                        alarmNotifyHistoryDto.AlarmTypeStr = AlarmType.Move.GetDescriptionString();

                        if (alarmDto.MoveAlarm.AlarmNotify.AppNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.APP;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.APP.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.MoveAlarm.AlarmMsgPre + (int)len + " 米";
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }
                        if (alarmDto.MoveAlarm.AlarmNotify.PhoneNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.PHONE;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.PHONE.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.MoveAlarm.AlarmMsgPre + (int)len + " 米";
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }
                        if (alarmDto.MoveAlarm.AlarmNotify.SMSNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.SMS;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.SMS.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.MoveAlarm.AlarmMsgPre + (int)len + " 米";
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }
                    }
                }
            }

            if (alarmDto.FenceAlarm.IsAlarm)
            {
                if (alarmDto.FenceAlarm.IsIntoAlarm)
                {
                    var fenceDto = fenceService.IntoFenceAlarm(alarmDto.CompanyId, alarmDto.AccountId, gpsInfoDto.Imei, gpsInfoDto.Lng, gpsInfoDto.Lat);
                    if (fenceDto != null)
                    {

                        alarmNotifyHistoryDto.AlarmType = AlarmType.Fence;
                        alarmNotifyHistoryDto.AlarmTypeStr = AlarmType.Fence.GetDescriptionString();
                        if (alarmDto.FenceAlarm.AlarmNotify.AppNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.APP;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.APP.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.FenceAlarm.IntoAlarmMsgPre + fenceDto.Name;
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }
                        if (alarmDto.FenceAlarm.AlarmNotify.PhoneNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.PHONE;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.PHONE.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.FenceAlarm.IntoAlarmMsgPre + fenceDto.Name;
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }
                        if (alarmDto.FenceAlarm.AlarmNotify.SMSNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.SMS;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.SMS.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.FenceAlarm.IntoAlarmMsgPre + fenceDto.Name;
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }
                    }
                }

                if (alarmDto.FenceAlarm.IsOutAlarm)
                {
                    if (fenceService.OutFenceAlarm(alarmDto.CompanyId, alarmDto.AccountId, gpsInfoDto.Imei, gpsInfoDto.Lng, gpsInfoDto.Lat))
                    {
                        alarmNotifyHistoryDto.AlarmType = AlarmType.Fence;
                        alarmNotifyHistoryDto.AlarmTypeStr = AlarmType.Fence.GetDescriptionString();
                        if (alarmDto.FenceAlarm.AlarmNotify.AppNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.APP;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.APP.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.FenceAlarm.OutAlarmMsgPre;
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }
                        if (alarmDto.FenceAlarm.AlarmNotify.PhoneNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.PHONE;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.PHONE.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.FenceAlarm.OutAlarmMsgPre;
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }
                        if (alarmDto.FenceAlarm.AlarmNotify.SMSNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.SMS;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.SMS.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.FenceAlarm.OutAlarmMsgPre;
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }
                    }
                }
            }

            if (alarmDto.LowPowerAlarm.IsAlarm)
            {
                if ((gpsInfoDto.deviceType == DeviceType.BAT42 && gpsInfoDto.BatteryVoltage < GeneralContainer.Bat42LowPowerAlarmVoltage) ||
                     gpsInfoDto.deviceType == DeviceType.BAT30 && gpsInfoDto.BatteryVoltage < GeneralContainer.Bat30LowPowerAlarmVoltage)
                {
                    alarmNotifyHistoryDto.AlarmType = AlarmType.LowPower;
                    alarmNotifyHistoryDto.AlarmTypeStr = AlarmType.LowPower.GetDescriptionString();
                    if (alarmDto.LowPowerAlarm.AlarmNotify.AppNotify.IsAlarm)
                    {
                        alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.APP;
                        alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.APP.GetDescriptionString();
                        alarmNotifyHistoryDto.NotifyMessage = alarmDto.LowPowerAlarm.AlarmMsgPre + gpsInfoDto.BatteryVoltage;
                        SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                    }

                    if (alarmDto.LowPowerAlarm.AlarmNotify.PhoneNotify.IsAlarm)
                    {
                        alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.PHONE;
                        alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.PHONE.GetDescriptionString();
                        alarmNotifyHistoryDto.NotifyMessage = alarmDto.LowPowerAlarm.AlarmMsgPre + gpsInfoDto.BatteryVoltage;
                        SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                    }
                    if (alarmDto.LowPowerAlarm.AlarmNotify.SMSNotify.IsAlarm)
                    {
                        alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.SMS;
                        alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.SMS.GetDescriptionString();
                        alarmNotifyHistoryDto.NotifyMessage = alarmDto.LowPowerAlarm.AlarmMsgPre + gpsInfoDto.BatteryVoltage;
                        SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                    }
                }
            }

            if(alarmDto.LightAlarm.IsAlarm)
            {
                if (gpsInfoDto.deviceType == DeviceType.BAT42 || gpsInfoDto.deviceType == DeviceType.BAT30)
                {
                    if(gpsInfoDto.WakeUpType == "LIGHT")
                    {
                        alarmNotifyHistoryDto.AlarmType = AlarmType.Light;
                        alarmNotifyHistoryDto.AlarmTypeStr = AlarmType.Light.GetDescriptionString();
                        if (alarmDto.LightAlarm.AlarmNotify.AppNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.APP;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.APP.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.LightAlarm.AlarmMsgPre;
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }

                        if (alarmDto.LightAlarm.AlarmNotify.PhoneNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.PHONE;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.PHONE.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.LightAlarm.AlarmMsgPre ;
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }
                        if (alarmDto.LightAlarm.AlarmNotify.SMSNotify.IsAlarm)
                        {
                            alarmNotifyHistoryDto.AlarmNotifyType = AlarmNotifyType.SMS;
                            alarmNotifyHistoryDto.NotifyTypeStr = AlarmNotifyType.SMS.GetDescriptionString();
                            alarmNotifyHistoryDto.NotifyMessage = alarmDto.LightAlarm.AlarmMsgPre;
                            SaveAlarmToMongoDB(alarmNotifyHistoryDto);
                        }
                    }
                }
            }
        }
        //SaveAlarmToMongoDB
        private void SaveAlarmToMongoDB(AlarmNotifyHistoryDto dto)
        {
            dto.Id = null;
            MongoDBCommandAlarmHistory.InsertDocumentAsync(dto);
            IsAlarm = true;
        }
        #endregion
         
    }
}
